<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/14 14:23
 * Desc: 有条件退保存退款规则
 */
class Listener_Order_Created_RefundRule extends Listener_Base
{
    public function handle($args)
    {
        // 有条件退保存订单退款规则
        if ($args['refund_restriction'] == 2) {
            $self       = new self();
            $self->args = $args;
            //判断队列是否开启,如果开启则把任务推送到队列
            if ($self->queue_status) {
                //推送到队列执行
                Queue::in('default', Listener_Order_Created_RefundRule::class, $args);
            } else {
                $self->perform();
            }
        }
    }

    public function perform()
    {
        $order             = $this->args;
        $refund_rule_class = Service_Refund_Rule_Factory::get_instance($order['typeid']);
        if ($refund_rule_class) {
            if($order['typeid'] == 5){
                $order['suitid'] = DB::select()->from('spot')->where('id','=',$order['productautoid'])->execute()->get('multiple_rule_ticket_id');
            }
            $rules = $refund_rule_class->get_refund_rule($order['suitid'], $order['productautoid']);
            if (!empty($rules)) {
                $model = ORM::factory('member_order_refund_rule')
                    ->where('order_id', '=', $order['id'])
                    ->find();
                if (!$model->id) {
                    $model          = ORM::factory('member_order_refund_rule');
                    $model->addtime = time();
                }

                $model->order_id = $order['id'];
                $model->rule     = serialize($rules);
                $model->save();
            }
            St_Log::write('保存退款规则', array('ordersn' => $order['ordersn']), 'event/order_created/log');
        }
    }

}